﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.mIE.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lamex.mIE.Model;
using Lamex.SIESystemManager.UI;
using Lamex.Common;
using System.Resources;
using System.Reflection;
using Lamex.mIE.zh_Hans;
using Lamex.mIE.en_us;
using Lamex.Library.CommonClass;

namespace Lamex.mIE.UI
{
    public partial class frmmIE_wkCenter_Definition : frmBaseChild
    {
        ResourceManager rm = null;
        DataTable dt = null;
        string isEdit = "";
        public frmmIE_wkCenter_Definition()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmIEWorkCenter_Load);
            gcView1.DoubleClick += new EventHandler(gcView1_DoubleClick);
            this.TextChanged += new EventHandler(frmIEWorkCenter_TextChanged);
            gcViewMain.OptionsBehavior.ReadOnly = true;
            gcViewMain.OptionsBehavior.Editable = false;
            
            //plRight.Visible = false;
        }
        private void frmIEWorkCenter_Load(object sender ,EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            ChangeLanguage();
            txtmIEWCEditProcessCode.UcReadOnly = true;
            this.txtmIEWCEditProductLine.Properties.Items.Clear();
            this.txtmIEWCEditProductLine.Properties.Items.Add("Laminate");
            this.txtmIEWCEditProductLine.Properties.Items.Add("Panel");
            this.txtmIEWCEditProductLine.Properties.Items.Add("Seating");
            this.txtmIEWCEditProductLine.Properties.Items.Add("MCG");

            this.txtmIEWCEditLogic.Properties.Items.Clear();
            this.txtmIEWCEditLogic.Properties.Items.Add("不适用");
            this.txtmIEWCEditLogic.Properties.Items.Add("全后台");
            this.txtmIEWCEditLogic.Properties.Items.Add("特征相关");
            this.txtmIEWCEditLogic.Properties.Items.Add("组包配");
         
        }
        private void frmIEWorkCenter_TextChanged(object sender,EventArgs e)
        {
            ChangeLanguage();
        }

        private void ChangeLanguage()
        {
            if (Loginer.CurrentUser.ChangeLanguage == 0)
            {
                rm = new ResourceManager(ResourcemIEZh.ResourceManager.BaseName, Assembly.GetExecutingAssembly());
           
            }
            else
            {
                rm = new ResourceManager(ResourcemIEEng.ResourceManager.BaseName, Assembly.GetExecutingAssembly());
            }

            this.Text = rm.GetString("frmIEWorkCenter");
            txtmIEWCEditProcessCode.Caption = rm.GetString("txtmIEWCEditProccess");
            txtmIEWCEditProcessName.Caption = rm.GetString("txtmIEWCEditProcessName");
            txtmIEWCEditProcessDesc.Caption = rm.GetString("txtmIEWCEditProcessDesc");
            txtmIEWCEditProductLine.Caption = rm.GetString("txtmIEWCEditProductLine");
            txtmIEWCEditLogic.Caption = rm.GetString("txtmIEWCEditLogic");
            txtmIEProcessCode.Caption= rm.GetString("txtmIEProcessCode");
            txtmIEProcessName.Caption = rm.GetString("txtmIEProcessName");
            txtmIEWCProductLine.Caption= rm.GetString("txtmIEWCProductLine");
            btnmIEWCSave.Text= rm.GetString("btnmIEWCSave"); 

            LanguageObject.RegisterLanguage(gcViewMain);
            gcChangeLanguage();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = " select * from IE_WorkCenter_Definition where 1=1 ";
            if (!string.IsNullOrEmpty(txtmIEProcessCode.Text))
            {
                sqlWhere += " and PROCESS_CODE like '%" + txtmIEProcessCode.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtmIEProcessName.Text))
            {
                sqlWhere += " and PROCESS_NAME like '%" + txtmIEProcessName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtmIEWCProductLine.Text))
            {
                sqlWhere += " and PRODUCT_LINE like '%" + txtmIEWCProductLine.Text + "%'";
            }
            sql = sql + sqlWhere;
            dt = BaseFactory<IE_WorkCenter_Definition>.Instance.GetDataTableBySql(sql);
            gcView1.DataSource = dt;
            base.DoView(sender);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        public override void DoAdd(IButtonInfo sender)
        {
            txtmIEWCEditProcessCode.UcReadOnly = false;
            isEdit = "New";
            gcChangeLanguage();
            base.DoAdd(sender);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        public override void DoEdit(IButtonInfo sender)
        {
            isEdit = "Edit";
            if (gcViewMain.RowCount < 1)
            {
                XtraMessageBox.Show(rm.GetString("txtmIEWCEditChooseMessage"));
                return;
            }
            gcChangeLanguage();
            //DataTable dtClone = dt.Clone();
            //DataRow dr = dt.NewRow();
            //dr = this.gcViewMain.GetDataRow(this.gcViewMain.FocusedRowHandle);
            //dtClone.Rows.Add(dr);
            //gcView2.DataSource = dtClone;
            //plRight.Visible = true;
            gcChangeLanguage();
            txtmIEWCEditProcessCode.UcReadOnly = true;
            base.DoEdit(sender);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        public override void DoSave(IButtonInfo sender)
        {
                base.DoSave(sender);
        }
        public override void DoImport(IButtonInfo sender)
        {
            FrmImport frm = new FrmImport("IE_WorkCenter_Definition");
            frm.ShowDialog();
            DoView(null);
            base.DoImport(sender);
        }
        public override void DoExport(IButtonInfo sender)
        {
            if (gcViewMain.RowCount < 1)
            {
                XtraMessageBox.Show(rm.GetString("txtmIEWCEditChooseMessage"));
                return;
            }
            SaveFileDialog pf = new SaveFileDialog();
            pf.Filter = "Excel文件(*.xls)|*.xls";
            pf.ShowDialog();
            if (string.IsNullOrEmpty(pf.FileName)) return;
            string path = pf.FileName;
            gcView1.ExportToXls(path);
            base.DoExport(sender);
        }
        public override void DoReturn(IButtonInfo sender)
        {
            //plRight.Visible = false;
            base.DoReturn(sender);
        }
        private void gcView1_DoubleClick(object sender , EventArgs e)
        {

            if (gcViewMain.RowCount < 1)
            {
                XtraMessageBox.Show(rm.GetString("txtmIEWCEditChooseMessage"));
                return;
            }
            int iRow = gcViewMain.FocusedRowHandle;
            txtmIEWCEditProcessCode.Text = gcViewMain.GetRowCellValue(iRow, "PROCESS_CODE").ToString();
            txtmIEWCEditProcessName.Text = gcViewMain.GetRowCellValue(iRow, "PROCESS_NAME").ToString();
            txtmIEWCEditProcessDesc.Text = gcViewMain.GetRowCellValue(iRow, "PROCESS_DESC").ToString();
            txtmIEWCEditProductLine.Text = gcViewMain.GetRowCellValue(iRow, "PRODUCT_LINE").ToString();
            txtmIEWCEditLogic.Text = gcViewMain.GetRowCellValue(iRow, "HOUR_LOGIC").ToString();
            isEdit = "Edit";
            gcChangeLanguage();
            txtmIEWCEditProcessCode.UcReadOnly = true;
        }
        private void gcChangeLanguage()
        {
            if (isEdit == "")
                gcmIWCEBox.Text = rm.GetString("txtmIEAddEdit");
            else if (isEdit == "New")
                gcmIWCEBox.Text = rm.GetString("txtmIEAdd");
            else if (isEdit == "Edit")
                gcmIWCEBox.Text = rm.GetString("txtmIEEdit");
        }

        private void btnmIEWCSave_Click(object sender, EventArgs e)
        {
            try
            {   
                if (txtmIEWCEditProcessCode.Alter()) return;
                if (txtmIEWCEditProcessName.Alter()) return;
                if (txtmIEWCEditProcessDesc.Alter()) return;
                if (txtmIEWCEditProductLine.Alter()) return;
                if (txtmIEWCEditLogic.Alter()) return;
                Lamex.mIE.Model.IE_WorkCenter_DefinitionInfo mod = BaseFactory<IE_WorkCenter_Definition>.Instance.GetEntriyList(" 1=1 and PROCESS_CODE ='"+txtmIEProcessCode.Text+"'").FirstOrDefault();
                if(mod!=null)
                {
                    XtraMessageBox.Show(rm.GetString("txtmIEWCMultipeMessage"));
                    return;
                }
                
                if (isEdit == "New")
                {

                    IE_WorkCenter_DefinitionInfo model = new IE_WorkCenter_DefinitionInfo();
                    model.PROCESS_CODE = txtmIEWCEditProcessCode.Text;
                    model.PROCESS_NAME = txtmIEWCEditProcessName.Text;
                    model.PROCESS_DESC = txtmIEWCEditProcessDesc.Text;
                    model.HOUR_LOGIC = txtmIEWCEditLogic.Text;
                    model.PRODUCT_LINE = txtmIEWCEditProductLine.Text;
                    if (BaseFactory<IE_WorkCenter_Definition>.Instance.InsertKey(model))
                    {
                        XtraMessageBox.Show(rm.GetString("txtmIESaveSuccess"));
                        isEdit = "";
                        txtmIEWCEditProcessCode.UcReadOnly = true;
                    }
                    else
                    {
                        XtraMessageBox.Show(rm.GetString("txtmIESaveFail"));
                    }
                    DoView(null);
                }
                if (isEdit == "Edit")
                {
                    IE_WorkCenter_DefinitionInfo model = new IE_WorkCenter_DefinitionInfo();
                    model.PROCESS_CODE = txtmIEWCEditProcessCode.Text;
                    model.PROCESS_NAME = txtmIEWCEditProcessName.Text;
                    model.PROCESS_DESC = txtmIEWCEditProcessDesc.Text;
                    model.HOUR_LOGIC = txtmIEWCEditLogic.Text;
                    model.PRODUCT_LINE = txtmIEWCEditProductLine.Text;
                    if (BaseFactory<IE_WorkCenter_Definition>.Instance.Update(model))
                    {
                        XtraMessageBox.Show(rm.GetString("txtmIESaveSuccess"));
                        isEdit = "";

                    }
                    else
                    {
                        XtraMessageBox.Show(rm.GetString("txtmIESaveFail"));

                    }

                    DoView(null);
                }

                gcChangeLanguage();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(rm.GetString("txtmIESaveFail"));
            }
        }
    }
    
}
